package org.linlinjava.litemall.admin.controller;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.beans.dto.ShopMerchandiseSetWarnNumDto;
import org.linlinjava.litemall.admin.service.ShopMerchandiseService;
import org.linlinjava.litemall.admin.vo.merchandise.MerchandiseImportExcelVO;
import org.linlinjava.litemall.admin.vo.shopmerchandise.ShopMerchandiseStatisticsReqVO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.excel.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
@Api("管理店铺商品控制器")
@RestController
@RequestMapping("/admin/shopMerchandise")
@Validated
public class AdminShopMerchandiseController {


    @Autowired
    private ShopMerchandiseService shopMerchandiseService;


    @LogAnno
    @GetMapping("/list")
    public Object list(String merchandiseSn, String name, @LoginAdminShopId Integer shopId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
                       Integer waning){
    	return shopMerchandiseService.list(shopId, name, merchandiseSn,waning, page, limit);
    }


    @ApiOperation("警告数量设置")
    @LogAnno
    @PostMapping("/setWarnNumber")
    public Object setWarnNumber(@Valid @RequestBody ShopMerchandiseSetWarnNumDto shopMerchandiseSetWarnNumDto){
        return shopMerchandiseService.setWarnNumber(shopMerchandiseSetWarnNumDto);
    }


    @ApiOperation("统计数据列表")
    @LogAnno
    @PostMapping("/statistics")
    public Object statistics(@Validated @RequestBody  ShopMerchandiseStatisticsReqVO reqVO,
                            @LoginAdminShopId Integer shopId){
        reqVO.setShopId(shopId);
    	return shopMerchandiseService.statistics(reqVO);
    }

    @ApiOperation("统计总数据")
    @LogAnno
    @PostMapping("/total-statistics")
    public Object totalStatistics(@Validated @RequestBody  ShopMerchandiseStatisticsReqVO reqVO){
        return shopMerchandiseService.totalStatistics(reqVO);
    }

    @PostMapping("/import")
    @ApiOperation("导入原料")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file",  required = true),
            @ApiImplicitParam(name = "shopId", example = "true")
    })
    public Object importExcel(@RequestParam("file") MultipartFile file,
                              @RequestParam(value = "shopId", required = false, defaultValue = "false") Integer shopId) throws Exception {
        List<MerchandiseImportExcelVO> list = ExcelUtils.read(file, MerchandiseImportExcelVO.class);
        return shopMerchandiseService.importMerchandiseList(list,shopId);
    }


    @GetMapping("/get-import-template")
    @ApiOperation("获得导入原料模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MerchandiseImportExcelVO> list = Arrays.asList(
                MerchandiseImportExcelVO.builder().serial("1001").classification("Beans")
                        .label("热卖").brand("Atlas Coffee Importers")
                        .name("Rosebud Espresso").standard("300g").baseUnit("g")
                        .checkUnit("unit").purchasePrice(new BigDecimal(4.68)).sellingPrice(new BigDecimal(4.914))
                        .number(10.00).unit("unit").ratio(1).checkRatio(300).build()
        );
        shopMerchandiseService.export(response, "原料导入模板.xls", "原料列表", list);
        // 输出
        //ExcelUtils.write(response, "原料导入模板.xls", "原料列表", MerchandiseImportExcelVO.class, list);
    }

}
